# coding:utf-8
# ui/go_enrichment_viewer.py

from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLabel, QTextEdit, QPushButton, QFileDialog
from PyQt5.QtGui import QFont
import os


class GoEnrichmentViewer(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.community_id = None
        self.init_ui()

    def init_ui(self):
        layout = QVBoxLayout(self)

        self.title_label = QLabel("GO 富集分析", self)
        self.title_label.setFont(QFont('Arial', 24, QFont.Bold))
        layout.addWidget(self.title_label)

        self.result_text = QTextEdit(self)
        self.result_text.setReadOnly(True)
        layout.addWidget(self.result_text)

        back_button = QPushButton("返回社区列表", self)
        back_button.clicked.connect(lambda: self.parent().switch_page(0))
        layout.addWidget(back_button)

        self.setLayout(layout)

    def set_community_id(self, community_id):
        self.community_id = community_id
        self.title_label.setText(f"GO 富集分析 - 社区 {community_id}")
        report_path = f"results/go_enrichment/go_enrichment_{community_id}.txt"

        if not os.path.exists(report_path):
            self.result_text.setText("暂无该社区的 GO 富集报告，请先运行分析脚本。")
            return

        with open(report_path, 'r') as f:
            content = f.read()
        self.result_text.setText(content)